#include "stdafx.h"
#include "U7FilesLoader.h"
#include <string>

#include "FlexFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


U7FilesLoader::U7FilesLoader()
{
	paletteFile = NULL;
	shapeFile = NULL;
	textFile = NULL;
	shpDimsFile = NULL;
	tfaFile = NULL;
	occludeFile = NULL;
	chunkFile = NULL;
	mapFile = NULL;
	fontFile = NULL;
	gumpFile = NULL;
	xformTblFile = NULL;
	u7nbufFile = NULL;
	u7ibufFile = NULL;

	memset( ifixFiles, 0, sizeof(ifixFiles) );
	memset( iregFiles, 0, sizeof(iregFiles) );
}

U7FilesLoader::~U7FilesLoader()
{
	if ( paletteFile )
		delete paletteFile;
	if ( shapeFile )
		delete shapeFile;
	if ( textFile )
		delete textFile;
	if ( shpDimsFile )
		delete shpDimsFile;
	if ( tfaFile )
		delete tfaFile;
	if ( occludeFile )
		delete occludeFile;
	if ( chunkFile )
		delete chunkFile;
	if ( mapFile )
		delete mapFile;
	if ( fontFile )
		delete fontFile;
	if ( gumpFile )
		delete gumpFile;
	if ( xformTblFile )
		delete xformTblFile;
	if ( u7nbufFile )
		delete u7nbufFile;
	if ( u7ibufFile )
		delete u7ibufFile;
	for ( int i = 0; i < 144; i++ )
	{
		if ( ifixFiles[i] )
			delete ifixFiles[i];
		if ( iregFiles[i] )
			delete iregFiles[i];
	}
}

void U7FilesLoader::Load( const char* rootPath )
{
	std::string root = rootPath;

	memoryAllocatedBytes = 0;

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\PALETTES.FLX").c_str(), &paletteFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\SHAPES.VGA").c_str(), &shapeFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\TEXT.FLX").c_str(), &textFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\SHPDIMS.DAT").c_str(), &shpDimsFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\TFA.DAT").c_str(), &tfaFile );

	// OCCLUDE.DAT = bit array (128 byte = $400 bit)
	// one bit per game shape. set if the shape completely occludes the space
	// it is covering (not sure?)
	memoryAllocatedBytes += Load( (root + "STATIC\\OCCLUDE.DAT").c_str(), &occludeFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\U7CHUNKS").c_str(), &chunkFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\U7MAP").c_str(), &mapFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\FONTS.VGA").c_str(), &fontFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\GUMPS.VGA").c_str(), &gumpFile );

	//
	memoryAllocatedBytes += Load( (root + "STATIC\\XFORM.TBL").c_str(), &xformTblFile );

	//
	memoryAllocatedBytes += Load( (root + "GAMEDAT\\U7NBUF.DAT").c_str(), &u7nbufFile, &u7nbufFileSize );
	memoryAllocatedBytes += Load( (root + "GAMEDAT\\U7IBUF.DAT").c_str(), &u7ibufFile );

	// IFIX files
	for ( int i = 0; i < 144; i++ )
	{
		char postfix[3];
		sprintf_s( postfix, "%02x", i );
		memoryAllocatedBytes += Load( (root + "STATIC\\U7IFIX" + postfix).c_str(), &ifixFiles[i] );
		memoryAllocatedBytes += Load( (root + "GAMEDAT\\U7IREG" + postfix).c_str(), &iregFiles[i], &iregFileSizes[i] );
	}

	if(0) // 356 npcs
	{
		char* initgame;
		Load( (root + "STATIC\\INITGAME.DAT").c_str(), &initgame );

		long length;
		char* npcdat = GetFlexArchiveFile( (PFLEXFILEHEADER)initgame, "npc.dat", &length );

		unsigned short* p = (unsigned short*)npcdat;
		unsigned npcCount1 = *p++;
		unsigned npcCount2 = *p;
		unsigned total = npcCount1 + npcCount2;

		delete initgame;
	}
}

unsigned U7FilesLoader::Load( const char* filename, char** buffer, long* fileSize )
{
	FILE* fp;
	long size = 0;
	errno_t err;

	*buffer = NULL;

	//
	err = fopen_s( &fp, filename, "rb" );
	if ( !err )
	{
		fseek( fp, 0, SEEK_END );
		size = ftell(fp);
		rewind(fp);

		*buffer = new char[size];
		if ( *buffer )
			fread( *buffer, 1, size, fp );
		else
			size = 0;

		if ( fileSize )
			*fileSize = size;

		fclose(fp);
	}
	else
	{
		TRACE( "Can't load file '%s' !!!\n", filename );
	}

	return size;
}
